
import page from '@/mixins/pages.js'
import { sandbox, downloadFile } from '@/libs/util'
import selectOptions from '@/publicComponents/selectOptions/index'


import searchAndReset from './searchAndReset'
export default {
  mixins: [page, searchAndReset],
  components: { selectOptions },
  data (vm) {
    let opt = {
      treeData: [],
      formField: {},
      tableData: [],
      drawerVisible: false,
      mode: '',
      loading: false,
      importLoading: false,
    }
    // 解决props和data有key值冲突的问题
    let tar = {}

    for (let key in opt) {
      if (!(key in vm)) {
        Object.assign(tar, { [key]: opt[key] })
      }
    }

    return tar
  },
  beforeDestroy () {
    this.formField &&
      typeof this.formField.reset === 'function' &&
      this.formField.reset()
  },
  methods: {
    /**首字母大写函数表示，将要被覆盖 */
    // 查询表格接口
    LoadTableDataMethod () {
      return Promise.resolve()
    },
    fetchData () {
      this.loading = true
      this.formField.pageIndex = this.pagination.pageIndex
      this.formField.pageSize = this.pagination.pageSize

      return this.LoadTableDataMethod()
        .then(res => {
          if (!res) return
          if ('ResultValue' in this) {
            this.ResultValue = res.ResultValue
          }

          this.tableData = res.Entity.Items || []
          this.pagination.pageIndex = res.Entity.pageIndex
          this.pagination.currentPage = res.Entity.pageIndex
          this.pagination.pageSize = res.Entity.pageSize
          this.pagination.total = res.Entity.TotalItemCount
        })
        .catch(() => {})
        .finally(() => {
          this.loading = false
        })
    },
    back(reLoad){
      this.mode = ''
      reLoad && this.fetchData()
    },
    closeDrawer(reLoad){
      this.visible = false
      this.drawerVisible = false
      reLoad && this.fetchData()
    },
    handleImport(){
      document.getElementById('upLoad').value = "";
      document.getElementById('upLoad').click();
    },
    importUpload(){
      let form = new FormData()
      let file = document.getElementById('upLoad');

      let files = file.files[0]; //获取的文件
      form.append('file', files)

      this.importLoading = true
      this.importMethod(form).then((res) => {
        this.$message({
          type: 'success',
          message: '导入成功'
        })
        this.fetchData()
      }).finally(()=>{
        this.importLoading = false
      })
    },
    download(url){
      if(url){
        const a = document.createElement('a')
        a.href = url
        a.download = '报告' // 下载后文件名
        a.style.display = 'none'
        document.body.appendChild(a)
        a.click() // 点击下载
        document.body.removeChild(a) // 下载完成移除元素
      }
    },
  }
}
